<?php
namespace Home\Controller;
use Think\Controller;
use Home\Model\ScoreModel;

class IndexController extends Controller {

    /**
     * 根据传递的省Id回去省以下的城市
     */
    function getCityByProId()
    {
        I('post.pro_id') ? 0 : die();
        $city = new \Home\Model\CityModel();
        $cityList = $city->getCityListByProId(I('post.pro_id'));
        header('Content-type:text/jsonl');
        echo json_encode($cityList);
    }

    /**
     * 根据传递的城市Id回去城市以下的医院
     */
    function getHospitalByCityId()
    {
        I('post.city_id') ? 0 : die();
        $hospital = new \Home\Model\HospitalModel();
        $hospitalList = $hospital->getHospitalListByCityId(I('post.city_id'));
        header('Content-type:text/jsonl');
        echo json_encode($hospitalList);
    }

    function getHospitalByProId()
    {
        I('post.pro_id') ? 0 : die();
        $hospital = new \Home\Model\HospitalModel();
        $hospitalList = $hospital->getHospitalListByProId(I('post.pro_id'));
        header('Content-type:text/jsonl');
        echo json_encode($hospitalList);
    }
    //医生
    public function yisheng(){
        $doctor = M()->table('hospital_doctor')->where(array('doc_id'=>$_GET['doc_id']))->find();
        $hos = M()->table('hospital_hospital')->where(array('hos_id'=>$doctor['doc_hos']))->find();
        $samehos = M()->table('hospital_doctor')->where(array('doc_hos'=>$doctor['doc_hos']))->select();
        foreach ($samehos as $key => $value) {
            $Pinyin = new \Home\Model\PinyinModel();
            $result = $Pinyin->TransformWithoutTone($samehos[$key]['doc_name']);
            $samehos[$key]['doc_url'] = 'https://'.$_SERVER["HTTP_HOST"].'/yisheng-'.$result.'-'.$samehos[$key]['doc_id'].'/';
        }

        $this->assign('samehos',$samehos);
        $doctor['doc_hosname'] = $hos['hos_name'];
        $doctor['doc_hosroute'] = $hos['hos_route'];
        $this->assign('doctor',$doctor);
        $this->display();
    }

    /**
     * 医院列表
     */
    public function HospitalList()
    {
        $provinceId = I('get.proId') ? I('get.proId') : 0;
        $cityId = I('get.cityId') ? I('get.cityId') : 0;
        $levelId = I('get.levelId') ? I('get.levelId') : 0;
        if($provinceId)
        {
            $provinceId = \Home\Model\ProvinceModel::getProvinceIdByPy($provinceId);
            $provinceId = $provinceId['pro_id'] ? $provinceId['pro_id'] : E('error',404);
        }
        if($cityId)
        {
            $cityInfo = \Home\Model\CityModel::getCityIdByPy($cityId);
            $cityId = $cityInfo['city_id'] ? $cityInfo['city_id'] : E('error',404);
            ($cityInfo['pro_id'] != $provinceId) ? E('error',404) : 0;
//            $provinceId = $provinceId['pro_id'] ? $provinceId['pro_id'] : E('error',404);
        }

        $hospitalList = \Home\Model\HospitalModel::getHospitalList($provinceId,$cityId,$levelId);
        $this->assign('hospitalList',$hospitalList);
        //获得省列表
        $province = new \Home\Model\ProvinceModel();
        $provinceList = $province->getProvinceList();

        foreach($provinceList as $provinceListKey => $provinceListValue)
        {
            if($provinceListValue['pro_id'] == $provinceId)
            {
                $this->assign('provinceName',$provinceListValue['pro_name']);
            }
        }
        $this->assign('provinceList',$provinceList);
        //获的省对应的城市列表
        $provinceId ? ($activeProvince['pro_id'] =$provinceId) : ($activeProvince = current($provinceList));
        if(isset($activeProvince['pro_id']))
        {
            $city = new \Home\Model\CityModel();
            $cityList = $city->getCityListByProId($activeProvince['pro_id']);
            $this->assign('cityList',$cityList);
            foreach($cityList as $cityListKey => $cityListValue)
            {
                if($cityListValue['city_id'] == $cityId)
                {
                    $this->assign('provinceName',$cityListValue['city_name']);
                }
            }
        }

        //获的城市对应的医院列表
        $cityId ? ($activeCity['city_id'] =$cityId) : ($activeCity = current($cityList));
        //if(!empty($activeCity['city_id']))
        if(!empty($cityId))
        {
            $hospital = new \Home\Model\HospitalModel();
            $hospitalList = $hospital->getHospitalListByCityId($activeCity['city_id']);
            $this->assign('hospitalList',$hospitalList);
            foreach($hospitalList as $hospitalListKey => $hospitalListValue)
            {
                if($hospitalListValue['hos_id'] == $hospitalId)
                {
                    $this->assign('cityName',$hospitalListValue['hos_name']);
                }
            }
        }

        //获得医院等级
        $level = new \Home\Model\LevelModel();
        $levelList = $level->getLevelList();

        //获得友情链接
        $linkInfo = M('link')->select();

        $this->assign('levelList',$levelList);
        $this->assign('provinceId',$provinceId);
        $this->assign('cityId',$cityId);
        $this->assign('levelId',$levelId);
        $this->assign('linkContent',$linkInfo);

        $this->display('HospitalList');
    }

    /**
     * 显示医院详情
     */
    function HospitalDetali()
    {   
        //获得医院详细信息
        $hospitalDetail = \Home\Model\HospitalModel::getHospitalDetail(I('get.hosId'));
        $hospitalDetail['hos_image'] = ltrim($hospitalDetail['hos_image'],".");
        $doctor = M()->table('hospital_doctor')->where(array('doc_hos'=>$_GET['hosId']))->select();
        foreach ($doctor as $key => $value) {
            $Pinyin = new \Home\Model\PinyinModel();
            $result = $Pinyin->TransformWithoutTone($doctor[$key]['doc_name']);
            $doctor[$key]['doc_url'] = 'https://'.$_SERVER["HTTP_HOST"].'/yisheng-'.$result.'-'.$doctor[$key]['doc_id'].'/';
        }

        $art = M()->table('hospital_art')->where(array('art_hospital_hos_id'=>$_GET['hosId']))->select();
        foreach ($art as $key => $value) {
            $art[$key]['art_url'] = 'https://'.$_SERVER["HTTP_HOST"].'/hos'.$hospitalDetail['hos_id'].'/art-'.$art[$key]['art_id'].'/';
        }

        //如果登陆则显示管理链接
        if (session('user') == 'admin' && session('pass') == 'qA0130') {
$gk_admin_qj = <<<qj
<p>
全局：
<a class="btn btn-default btn-xs" href="https://yiyuan.snsnb.com/index.php/Home/Backstage/listdateLink">新增友情链接</a> | 
<a class="btn btn-default btn-xs" href="https://yiyuan.snsnb.com/index.php/Home/Backstage/addHospital">添加医院信息</a> | 
<a class="btn btn-default btn-xs" href="https://yiyuan.snsnb.com/index.php/Home/Backstage/Wlistlist">文章列表</a> | 
<a class="btn btn-default btn-xs" href="https://yiyuan.snsnb.com/index.php/Home/Backstage/addDoc">添加医生信息</a> | 
<a class="btn btn-default btn-xs" href="https://yiyuan.snsnb.com/index.php/Home/Backstage/addWlist">添加文章</a> | 

qj;

$gk_admin_dq = <<<dq
<p>
当前：
<a class="btn btn-default btn-xs" href="http:'.'//api-admin.snsnb.com/admin/index.php/Cj/Rule/addRuleKeywords/title_content_name/'{$hospitalDetail['hos_id']}':'{$hospitalDetail['hos_name']}'/title_content/'{$hospitalDetail['hos_name']}'/cjy_bm/0/listgz_id/23">文章采集（局域网支持）</a> | 
<a class="btn btn-default btn-xs" href="https://yiyuan.snsnb.com/index.php/Home/Backstage/updateHospital/hosId/{$_GET['hosId']}">修改</a> | 
</p>
dq;

// <a href="http:'.'//api-admin.snsnb.com/admin/index.php/Cj/Rule/addRuleKeywords/title_content_name/'{$hospitalDetail['hos_id']}':'{$hospitalDetail['hos_name']}'/title_content/'{$hospitalDetail['hos_name']}'/cjy_bm/0/listgz_id/23">文章采集（局域网支持）</a> | 


         $addyiyuan = $gk_admin_qj.$gk_admin_dq;
        }
        $this->assign('addyiyuan',$addyiyuan);

        $this->assign('doctorList',explode(',',$hospitalDetail['doctor_name']));
        $this->assign('doctor',$doctor);
        $this->assign('art',$art);
        $this->assign('hospitalDetail',$hospitalDetail);
        //获得同省其他医院信息
        $otherSql = 'SELECT a.hos_id,a.hos_name,a.hos_url,a.hos_detailed_address
FROM
hospital_hospital AS a
WHERE a.pro_id = '.$hospitalDetail['pro_id'].' and a.hos_id != '.$hospitalDetail['hos_id'].' limit 0,5';
        $this->assign('otherHospltal',M()->query($otherSql));
        //面包屑
        $breadNav = \Home\Model\HospitalModel::getbreadNavByHosId(I('get.hosId'));
        $this->assign('breadNav',$breadNav);
//        $score = ScoreModel::getScoreByHospitalID(1,3);
//        dump($score);
        $this->display('HospitalDetali');       
    }

    /**
     * 显示医院文章详情
     */
    public function Wlistcont(){
        $User = M("art"); 
        $date['art_id'] = $_GET['artid'];
        $artcont = $User->where($date)->find();
        $artcont['art_cont'] = htmlspecialchars_decode(htmlspecialchars_decode($artcont['art_cont']));
        $this->assign('artcont',$artcont);
        $hosid = M("art")->where(array('art_id'=>I('get.artid')))->getField('art_hospital_hos_id');
        // 面包屑
        $breadNav = \Home\Model\HospitalModel::getbreadNavByHosId($hosid,I('get.artid'));
        $this->assign('breadNav',$breadNav);
        $this->display('Wlistcont');
    }

    /**
     * atc.cn提交的医院文章//权限待解决
     */
    public function addyiyuancontent(){
        var_dump($_POST);
        $User = M("art"); // 实例化User对象
        // 根据表单提交的POST数据创建数据对象
        $User->create();
        $User->add();
    }

    public function hospitalListJson()
    {
        $provinceId = I('get.proId') ? I('get.proId') : 0;
        $cityId = I('get.cityId') ? I('get.cityId') : 0;
        $levelId = I('get.levelId') ? I('get.levelId') : 0;
        $hospitalList = \Home\Model\HospitalModel::getHospitalList($provinceId,$cityId,$levelId);
        foreach ($hospitalList as $index => $item) {
            $hospitalList[$index]['hos_image'] = 'https://' . $_SERVER['HTTP_HOST'] . str_replace('./','/',$item['hos_image']);
        }
        header('Content-type: application/json');
        $this->ajaxReturn($hospitalList,'jsonp');
    }

    public function hospitalDetailJson()
    {
        $result = array();
        if(I('get.hosId'))
        {
            $hospitalDetail = \Home\Model\HospitalModel::getHospitalDetail((int)I('get.hosId'));
            if(isset($hospitalDetail['hos_image']))
            {
                $hospitalDetail['hos_image'] = 'https://' . $_SERVER['HTTP_HOST'] . str_replace('./','/',$hospitalDetail['hos_image']);
                $result = $hospitalDetail;
            }
        }
        header('Content-type: application/json');
        $this->ajaxReturn($result,'jsonp');
    }
    public function provinceListJson()
    {
        //获得省列表
        $province = new \Home\Model\ProvinceModel();
        $provinceList = $province->getProvinceList();
        header('Content-type: application/json');
        $this->ajaxReturn($provinceList,'jsonp');
    }
    public function cityListJson()
    {
        $result = array();
        if(I('get.proId'))
        {
            $city = new \Home\Model\CityModel();
            $result = $city->getCityListByProId((int)I('get.proId'));
        }
        header('Content-type: application/json');
        $this->ajaxReturn($result,'jsonp');
    }
}